What is np nonzero?

In NumPy, the np.nonzero() function returns the indices of the elements in an array where the value is non-zero. It returns a tuple of arrays, one for each dimension of the input array, containing the indices where the non-zero elements are present.

For example, if we have an array a = np.array([0, 1, 0, 3, 4]), then np.nonzero(a) would return (array([1, 3, 4]),), which means that the non-zero elements are present at indices 1, 3 and 4.

Similarly, if we have a 2D array b = np.array([[0, 1, 0], [3, 0, 4]]), then np.nonzero(b) would return a tuple of two arrays (array([0, 1]), array([1, 0, 2])), which means that the non-zero elements are present at the indices (0,1), (1,0), and (1,2).

np.nonzero() is a useful function for working with sparse matrices and filtering data based on non-zero values.